package com.lbn.monitor.app.monitor.facility;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.free.framework.mvp.BaseMvpPresenter;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Pollutant;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.api.model.XiShu;
import com.lbn.monitor.app.monitor.DataCallBack;
import com.lbn.monitor.base.AppBaseFragment;
import com.lbn.monitor.util.DataUtils;
import com.lbn.monitor.util.String2Num;
import com.lbn.monitor.util.UIHandler;
import com.lbn.monitor.widget.NumCTextView;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NcFragment extends AppBaseFragment implements DataCallBack {

    private static final String TAG = "NcFragment";

    @BindView(R.id.so2)
    NumCTextView so2;
    @BindView(R.id.so2C)
    NumCTextView so2C;
    @BindView(R.id.nox)
    NumCTextView nox;
    @BindView(R.id.noxC)
    NumCTextView noxC;
    @BindView(R.id.o2)
    NumCTextView o2;
    @BindView(R.id.dust)
    NumCTextView dust;
    @BindView(R.id.dust_c)
    NumCTextView dustC;
    @BindView(R.id.humidity)
    NumCTextView humidity;
    @BindView(R.id.temperature)
    NumCTextView temperature;
    @BindView(R.id.pressure)
    NumCTextView pressure;
    @BindView(R.id.airflow)
    NumCTextView airflow;


//    so2.setText(data.getStr02_Rtd());
//    nox.setText(data.getStr03_Rtd());
//    o2.setText(data.getS01_Rtd());
//    dust.setText(data.getStr01_Rtd());
//    humidity.setText(data.getS05_Rtd());
//    temperature.setText(data.getS03_Rtd());
//    pressure.setText(String.valueOf(Double.valueOf(data.getS08_Rtd())*1000000));

    private double str02standard;
    private double str03standard;
    private double s01standard;
    private double str01standard;
    private double s05standard;
    private double s03standard;
    private double s08standard;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nc, null, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        if (rootView == null) {
            for (Pollutant p : DataUtils.getPollutants(getContext())) {
                int id = Integer.parseInt(p.getId());
                switch (id) {
                    case 1:
                        str01standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 2:
                        str02standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 3:
                        str03standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 11:
                        s01standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 13:
                        s03standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 15:
                        s05standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                    case 18:
                        s08standard = String2Num.str2Double(p.getStandardvalue());
                        break;
                }
            }
            ((FacilityDetailActivity) getActivity()).addCallBack(this);
        }
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    public static NcFragment newInstance() {
        Bundle args = new Bundle();
        NcFragment fragment = new NcFragment();
        fragment.setArguments(args);
        return fragment;
    }

//    @Override
//    public void callBack(RealTimeValue data) {
//
//        getData();
//
//        FacilityDetailActivity activity = (FacilityDetailActivity) getActivity();
//        if (activity == null)
//            return;
//
//        so2.setText(data.getStr02_Rtd(),str02standard);
//        so2C.setText(data.getStr02_ZsRtd());
//        nox.setText(data.getStr03_Rtd(),str03standard);
//        noxC.setText(data.getStr03_ZsRtd());
//        o2.setText(data.getS01_Rtd(),s01standard);
//        dust.setText(data.getStr01_Rtd(),str01standard);
//        dustC.setText(data.getStr01_ZsRtd());
//        humidity.setText(data.getS05_Rtd(),s05standard);
//        temperature.setText(data.getS03_Rtd(),s03standard);
//        pressure.setText(String.valueOf(Double.valueOf(data.getS08_Rtd()) * 1000000),s08standard);
//
//        XiShu xiShu = activity.getXishu();
//        if (xiShu == null)
//            return;
//
//        double xiShuValue = Double.valueOf(xiShu.getXiShuValue());
//        double airflowValue = 0;
//
//        Log.e(TAG, Double.valueOf(xiShu.getYanDaoMianJi()) + "");
//        Log.e(TAG, Double.valueOf(data.getB02_Rtd()) + "");
//        Log.e(TAG, Double.valueOf(Double.valueOf(data.getS03_Rtd())) + "");
//        Log.e(TAG, Double.valueOf(Math.abs(Double.valueOf(data.getS08_Rtd()))) + "");
//        Log.e(TAG, Double.valueOf(data.getS05_Rtd()) + "");
//
//        if (xiShuValue > 0) {
////            3600 * (表liuliangxishu中的列YanDaoMianJi对应的值) * B02_Rtd * 273 / (273 + S03_Rtd) * (89600 + Abs(S08_Rtd) * 1000000) / 101325 * (1 - S05_Rtd / 100) * 系数
//            airflowValue = 3600 * Double.valueOf(xiShu.getYanDaoMianJi()) * Double.valueOf(data.getB02_Rtd()) * 273 / (273 + Double.valueOf(data.getS03_Rtd())) * (89600 + Math.abs(Double.valueOf(data.getS08_Rtd())) * 1000000) / 101325 * (1 - Double.valueOf(data.getS05_Rtd()) / 100) * xiShuValue;
//        } else {
////            3600 * (表liuliangxishu中的列YanDaoMianJi对应的值) * B02_Rtd * 273 / (273 + S03_Rtd) * (89600 + Abs(S08_Rtd) * 1000000) / 101325 * (1 - S05_Rtd / 100)
//            airflowValue = 3600 * Double.valueOf(xiShu.getYanDaoMianJi()) * Double.valueOf(data.getB02_Rtd()) * 273 / (273 + Double.valueOf(data.getS03_Rtd())) * (89600 + Math.abs(Double.valueOf(data.getS08_Rtd())) * 1000000) / 101325 * (1 - Double.valueOf(data.getS05_Rtd()) / 100);
//        }
//        Log.e(TAG, airflowValue + "");
//        airflow.setText(new DecimalFormat("#.00").format(airflowValue));
//    }

    @Override
    public void callBack(RealTimeValue data) {

    }

    @Override
    public void clear() {

    }


    private void getData(){
        UIHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                FacilityDetailActivity activity = ((FacilityDetailActivity) getActivity());
                if (activity == null) {
                    return;
                }
                activity.getData();
            }
        }, 1000);
    }

}
